package com.PQLoad{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.utils.ByteArray;

	public class PQLoader{
		static private var _loadedNum:int;
		static private var _totalNum:int;
		public function PQLoader()
		{
		}
		/**
		 *加载对象
		 * @param url 地址 
		 * @param obj 相关参数 
		 * @param level 优先级 
		 * @param cache 是否缓存，默认为是
		 * @param obj_onOpen 开始加载回调方法
		 * @param obj_onOpenParam 开始加载时回调方法的参数Object
		 * @param obj_onProgress 加载过程中回调的方法Function
		 * @param obj_onProgressParam 加载过程中回调方法的参数Object
		 * @param obj_onComplete 加载结束后回调的方法Function 不带参数
		 * @param obj_onCompleteParam 加载结束后回调方法的参数Object
		 * @param obj_onError 加载失败后回调的方法Function
		 * @param obj_onErrorParam 加载失败后回调方法的参数Object
		 * @return 加载完成后通过get序列方法获取资源
		 * @author 56lea 
		 **/
		static public function load(url:String,param:Object,level:int=10,cache:Boolean=true):void{
			var v:LoadVo=new LoadVo();
			v.url=url;
			v.level=level;
			v.cache=cache;
			if(param.parent)v.parent=param.parent;
			if(param.width)v.width=param.width;
			if(param.height)v.height=param.height;
			if(param.onOpen)v.onOpen=param.onOpen;
			if(param.onOpenParam)v.onOpenParam=param.onOpenParam;
			if(param.onProgress)v.onProgress=param.onProgress;
			if(param.onProgressParam)v.onProgressParam=param.onProgressParam;
			if(param.onComplete)v.onComplete=param.onComplete;
			if(param.onCompleteParam)v.onCompleteParam=param.onCompleteParam;
			if(param.onError)v.onError=param.onError;
			if(param.onErrorParam)v.onErrorParam=param.onErrorParam;
			PQLoad.load(v);
		}
		static public function loadList(url:Array,param:Object,level:int=10,cache:Boolean=true):void{
			var n:int=url.length;
			_totalNum=n;
			var count:int=0;
			for(var i:int=0;i<n;i++){
				load(url[i],{onComplete:counts,onError:counts},level,cache);
			}
			function counts():void{
				count++;
				_loadedNum=count;
				if(param.onProgress){
					param.onProgress(_loadedNum/_totalNum);
				}
				if(count==n){
					_loadedNum=0;
					_totalNum=1;
					if(param.onComplete){
						if(param.onCompleteParam)param.onComplete(param.onCompleteParam);
						else param.onComplete();
					}
				}
			}
		}
		static public function get progress():Number{
			return PQLoad.loadedSize/PQLoad.totalSize;
		}
		static public function get loadNum():int{
			return PQLoad.loadedNum;
		}
		static public function get totalNum():int{
			return PQLoad.totalNum;
		}
		/**
		 *获取图片或SWF资源 
		 * @param url
		 * @param callBack
		 */		
		static public function getRes(url:String,callBack:Function):void{
			if(FileCheck.checkUrl(url)==FileCheck.IMG)callBack.apply(null,[getImg(url)]);
			else if(FileCheck.checkUrl(url)==FileCheck.SWF)getSwf(url,callBack);
			else throw new Error("资源格式错误！");
		}
		static public function getLoad(url:String):Object{
			return LoadCache.self.getCache(url);
		}
		/**获取图片资源**/
		static public function getImg(url:String):DisplayObject{
			return LoadCache.self.getCache(url).copy();
		}
		/**获取PNG图片非透明区域**/
		static public function getPng(url:String):DisplayObject{
			var obj:Sprite=new Sprite();
			var img:DisplayObject=getImg(url);
			var bit:BitmapData = new BitmapData(img.width,img.height,true,0x00000000);
			bit.draw(img);
			var sp:Sprite=new Sprite();
			sp.graphics.beginFill(0);
			for(var x:uint=0;x<bit.width;x++){
				for(var y:uint=0;y<bit.height;y++){
					if(bit.getPixel32(x,y))sp.graphics.drawRect(x,y,1,1);
				}
			} 
			sp.graphics.endFill();
			obj.addChild(img);
			obj.addChild(sp);
			img.mask=sp;
			return obj;
		}
		/**获取SWF资源，注意：本方法为直接获取数据源，会影响后续引用，一般用于取一次性资源用的**/
		static public function getSwfRes(url:String):DisplayObject{
			return LoadCache.self.getCache(url) as DisplayObject;
		}
		/**同步获取SWF资源副本**/
		static public function getSwf(url:String,callBack:Function,param:Object=null):void{
			LoadCache.self.getCache(url).copy(callBack,param);
		}
		/**获取XML数据**/
		static public function getXml(url:String):XML{
			return LoadCache.self.getCache(url).Data;
		}
		/**获取SWF库中的类**/
		static public function getClassByLibrary(url:String,className:String):Class{
			return LoadCache.self.getCache(url).getClassByLibrary(className);
		}
		/**获取SWF中通过Embed嵌入的类**/
		static public function getClassByEmbed(url:String,className:String):Class{
			return LoadCache.self.getCache(url).getClassByEmbed(className);
		}
		/**删除指定缓存**/
		static public function removeCache(url:String):void{
			LoadCache.self.removeCache(url);
		}
		/**清空所有缓存**/
		static public function clear():void{
			LoadCache.self.clear();
		}
	}
}